/**
 * @Project Name:springboot-sentinel
 * @Package Name:cn.hoofungson.springboot.sentinel.exception
 * @Since JDK 1.8
 */
package cn.hoofungson.studio.sentinel.dubbo.service.provider.util.exception;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 异常工具
 * @Author 胡松 hoofungson@163.com
 * @Date 2019-05-29 14:32
 * @Version V1.0
 */
@Slf4j
public class ExceptionUtil {

    /**
     * Fallback 函数，函数签名与原函数一致或加一个 Throwable 类型的参数.
     *
     * @param username
     * @return
     */
    public static String helloFallback(String username) {
        return String.format("Halooooo %d", username);
    }

    /**
     * Block 异常处理函数，参数最后多一个 BlockException，其余与原函数一致.
     *
     * @param username
     * @param ex
     * @return
     **/
    public static String exceptionHandler(String username, BlockException ex) {
        // Do some log here.
        if (ex instanceof FlowException) {
            log.error("触发限流异常:", ex);
        }
        return "Oops, error occurred at " + username;
    }
}
